package com.heima.admin.contrller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.service.ApUserRealnameService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.PageDto;
import com.heima.model.admin.pojos.ApUserRealname;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/v1/auth")
public class AdminCheckController {
    @Autowired
    private ApUserRealnameService apUserRealnameService;
    @PostMapping("/list")
    public ResponseResult list(@RequestBody PageDto pageDto, HttpServletRequest request){
        //权限识别
        String userId = request.getHeader("userId");
        if (!"3".equals(userId)){
            throw new  CustomException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        Page<ApUserRealname> page = new Page<>();
        page.setPages(pageDto.getPage());
        page.setSize(pageDto.getSize());
        if (pageDto.getStatus()!=null){
            LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUserRealname::getStatus,pageDto.getStatus());
            Page<ApUserRealname> page1 = apUserRealnameService.page(page, queryWrapper);
            ResponseResult result = new PageResponseResult(pageDto.getPage(),pageDto.getSize(),(int)page1.getTotal());

            result.setData(page1.getRecords());
            return result;
        }else {
            Page<ApUserRealname> page1 = apUserRealnameService.page(page);
            ResponseResult result = new PageResponseResult(pageDto.getPage(),pageDto.getSize(),(int)page1.getTotal());

            result.setData(page1.getRecords());
            return result;
        }


    }
    @RequestMapping("/authFail")
    public ResponseResult authFail(@RequestBody PageDto pageDto,HttpServletRequest request){
        //权限识别
        String userId = request.getHeader("userId");
        if (!"3".equals(userId)){
            throw new  CustomException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        apUserRealnameService.authFail(pageDto);
        return ResponseResult.okResult("驳回成功");
    }
    @RequestMapping("/authPass")
    public ResponseResult authPass(@RequestBody PageDto pageDto,HttpServletRequest request){
        //权限识别
        String userId = request.getHeader("userId");
        if (!"3".equals(userId)){
            throw new  CustomException(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        apUserRealnameService.authPass(pageDto);
        return ResponseResult.okResult("通过成功");
    }

}
